<!--
 * @Description: 业务管理系统
 * @Author: fxg
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-25 17:52:23
-->
<template>
  <div class="pages_content table taskyTypes">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <transition name="from"></transition>
            <span v-show="!showCardNav"><i style="margin-right: 5px;" class="el-icon-arrow-down"></i></span>
            <transition name="to"></transition>
            <span v-show="showCardNav"><i style="margin-right: 5px;" class="el-icon-arrow-up"></i></span>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">名称：</span>
                <el-input v-model="tableParam.name" style="width: 200px;" clearable placeholder="请输入" />
              </span>
              <span style="margin-left: 36px;">
                <span style="margin-right: 12px;">系统</span>
                <el-select v-model="tableParam.systemId" placeholder="请选择" style="width: 200px;">
                  <el-option v-for="item in businessarr" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </span>
              <span style="margin-left: 36px;">
                <span style="margin-right: 12px;">编码</span>
                <el-input v-model="tableParam.code" style="width: 200px;" clearable />
              </span>

              <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), queryPage()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" @click="reset()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button size="small" class="bottom_all" style="margin-left: 0;" @click="add()">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button size="small" class="bottom_two" @click="batchUpdateOrDelete()">
                <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                删除
              </el-button>
            </div>
            <div>
              <el-link :underline="false" @click="updateSortNum()">
                <span class="el-icon--left"><i class="iconfont iconicon_button_sort1"></i></span>
                保持排序号
              </el-link>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left"><i class="iconfont iconicon_button_filtrate"></i></span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link :underline="false" @click="$Download(DownloadURL, tableParam)">
                <span class="el-icon--left"><i class="iconfont iconicon_button_export1"></i></span>
                导出
              </el-link>
            </div>
          </div>
          <div class="card_info" :style="style">
            <el-table ref="multipleTable" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection" width="50"></el-table-column>
              <template v-for="(item, index) in table">
                <el-table-column v-if="item.status" :key="index" :label="item.label" align="center" :width="item.width">
                  <template slot-scope="scope">
                    <el-button v-if="item.label == '操作'" class="bottom_one" size="mini" @click="update(scope.row)">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                    <span v-else-if="item.label == '所属系统'">{{ businessarr | fromatTable(scope.row.systemId, 'name', 'id') }}</span>
                    <span v-else-if="item.label == '数据来源'">{{ dataSourcearr | fromatTable(scope.row.dataSource, 'name', 'id') }}</span>
                    <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
          <div class="card_bottom">
            <div class="card_bottom_left">
              <div class="top_page_totle">总共{{ total }}条</div>
              <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="queryPage()"></el-pagination>
            </div>
            <div>
              <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="queryPage()">刷新</el-button>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="480px" :close-on-click-modal="false" :before-close="resefrom">
      <span slot="title" class="dialog-title">{{ dialogtitle }}</span>
      <div class="dialoginfo">
        <el-form ref="dialogForm" :rules="rules" :inline="true" :model="from" label-width="90px">
          <el-form-item prop="name">
            <span slot="label" style="color:  #FF0000 ;">名称</span>
            <el-input v-model="from.name" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <span slot="label" style="color:  #FF0000 ;">编码</span>
            <el-input v-model="from.code" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="systemId">
            <span slot="label">所属系统</span>
            <el-select v-model="from.systemId" placeholder="请选择">
              <el-option v-for="item in businessarr" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="dataSource">
            <span slot="label">数据源</span>
            <el-select v-model="from.dataSource" placeholder="请选择">
              <el-option v-for="item in dataSourcearr" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="from.dataSource == 1" prop="dataSourceValues">
            <span slot="label">数据源配置</span>
            <el-select v-model="from.dataSourceValues" filterable placeholder="请选择">
              <el-option v-for="item in dataSourceValuesarr" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="from.dataSource !== 1" prop="dataSourceValues">
            <span slot="label">数据源配置</span>
            <el-input v-model="from.dataSourceValues" class="textarea" placeholder="请输入数据源配置(格式为：${'key' <= 'value'})" type="textarea" resize="none" rows="5" clearable />
          </el-form-item>
          <el-form-item prop="sortNum">
            <span slot="label">排序号</span>
            <el-input v-model="from.sortNum" placeholder="请输入排序号" type="Number" clearable />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom()">取 消</el-button>
        <el-button class="bottom_all" @click="save()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import breaDcrumb from '@/components/breadcrumb';
import { queryPage as querybusinessPage } from '@/api/basedata/business.js';
import { mapGetters } from 'vuex';
import { queryPage, saveOrUpdate, updateSortNum, batchUpdateOrDelete, Download, } from '@/api/Signed/DieEemente.js';
import { getDictionaryData } from '@/api/basedata/dictionary';
export default {
  components: {
    breaDcrumb
  },
  data() {
    return {
      DownloadURL: Download,
      /*判断收起展开*/
      dialogVisible: false,
      dialogtitle: '添加审批要素',
      showCardNav: true,
      businessarr: [],
      dataSourcearr: [{ name: '数据字典', id: 1 }, { name: 'SQL定义', id: 2 }, { name: '手工录入', id: 3 }],
      dataSourceValuesarr: [],
      /*搜索关键字段*/
      /*列表展示*/
      table: [
        { label: '编码', width: 'auto', prop: 'code', status: true },
        { label: '名称', width: 'auto', prop: 'name', status: true },
        { label: '所属系统', width: 'auto', prop: 'systemName', status: true },
        { label: '数据来源', width: 'auto', prop: 'dataSource', status: true },
        { label: '数据来源配置', width: 'auto', prop: 'dataSourceValues', status: true },
        { label: '排序号', width: '120', prop: 'sortNum', status: true },
        { label: '操作', width: '120', prop: '', status: true }
      ],

      from: {
        name: '',
        code: '',
        systemId: '',
        dataSource: '',
        dataSourceValues: '',
        sortNum: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
        systemId: [{ required: true, message: '请选所属系统', trigger: 'change' }],
        dataSource: [{ required: true, message: '请选数据源', trigger: 'change' }],
        dataSourceValues: [{ required: true, trigger: 'change' }],
        sortNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      /*列表选中的数据集*/
      tableParam: {
        page: 1,
        pageSize: 12,
        code: '',
        systemId: '',
        name: ''
      },
      ids: [],
      sortIds: [],
      total: 0,
      tableData: []
    };
  },
  mounted() {
    let params = {
      page: 1,
      pageSize: 10000000
    };
    querybusinessPage(params).then(res => {
      this.businessarr = res.data.data;
    });
    getDictionaryData(params).then(res => {
      this.dataSourceValuesarr = res.data.data;
    });
    this.queryPage();
  },
  methods: {
    save() {
      let _this = this;
      this.$refs.dialogForm.validate(valid => {
        console.log(valid)
        if (valid) {
          if (_this.from.dataSource == 1) {
            let obj = _this.dataSourceValuesarr.find(function (item) {
              return item.code == _this.from.dataSourceValues;
            });
            _this.from.dataSourceValues = obj.code;
          }
          saveOrUpdate({ ..._this.from }).then(() => {
            _this.queryPage();
            _this.resefrom();
          });
        } else {
          return false;
        }
      });
    },
    /*获取数据*/
    queryPage() {
      queryPage(this.tableParam).then(res => {
        this.total = res.data.totalRows;
        this.tableData = res.data.data;
      });
    },
    /*重置*/
    reset() {
      this.tableParam.code = '';
      this.tableParam.systemId = '';
      this.tableParam.name = '';
      this.queryPage();
    },
    resefrom() {
      this.dialogVisible = false;
      this.$refs.dialogForm.resetFields();
    },
    updateSortNum() {
      updateSortNum(this.sortIds).then(() => {
        this.queryPage();
      });
    },
    add() {
      this.dialogtitle = '添加审批要素';
      this.dialogVisible = true;
    },
    update(data) {
      let str = JSON.stringify(data);
      this.from = JSON.parse(str);
      this.dialogtitle = '修改审批要素';
      this.dialogVisible = true;
      str = null;
    },
    /*删除*/
    batchUpdateOrDelete() {
      if (this.ids.length == 0) {
        this.$message({
          message: '请选择要操作的数据项',
          type: 'warning'
        });
        return;
      }
      this.$confirm('此操作将永久删除该信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      })
        .then(() => {
          batchUpdateOrDelete({
            ids: this.ids,
            status: 0,
            type: 'update_status'
          }).then(() => {
            this.reset();
          });
        })
        .catch(() => { });
    },
    /*批量选择*/
    handleSelectionChange(val) {
      this.ids = [];
      this.sortIds = [];
      val.forEach(item => {
        this.ids.push(item.id);
        this.sortIds.push({ id: item.id, sortNum: item.sortNum });
      });
    },
  },
  computed: {
    ...mapGetters({
      screenWidth: 'getscreenWidth',
      screenHeight: 'getscreenHeight',
      Collapse: 'getsCollapse'
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 124;
      } else {
        num = this.screenWidth - 220;
      }
      return 'width:' + num + 'px;';
    }
  }
};
</script>
<style lang="scss" scoped>
.taskyTypes {
  .dialoginfo {
    .el-input,
    .el-select {
      width: 300px;
    }
    .textarea_input {
      padding-top: 10px;
      align-items: flex-start;
      height: 118px;
    }
    .textarea {
      width: 300px;
    }
  }
}
</style>
